package com.itheiam.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.itheiam.domain.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("/user")
public class UsersServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取前端get提交方式提交的参数信息
//        String username = req.getParameter("username");
//        String pwd = req.getParameter("pwd");
//        String userStr = req.getParameter("user");
//        System.out.println(userStr);
        //将json格式字符串转换成对象
//        ObjectMapper om = new ObjectMapper();
//        User user = om.readValue(userStr, User.class);
//        System.out.println(user);

//        resp.getWriter().write(username);
//        resp.getWriter().write(pwd);

        //以字符流读取请求体中的数据
        BufferedReader reader = req.getReader();
        Gson gson = new Gson();
        //json格式的字符串数据
        User user = gson.fromJson(reader, User.class);
        System.out.println(user);
        resp.getWriter().write("ok");
    }
}
